package com.lianbang.staff.module.shop

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.lifecycle.Observer
import cn.ubest.mvvmcore.base.BaseListActivity
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.lianbang.staff.R
import com.lianbang.staff.adapter.ShopDetailListAdapter
import com.lianbang.staff.bean.ShopDetailBean
import com.lianbang.staff.bean.ShopOrderListBean
import com.lianbang.staff.databinding.ActivityShopDetailBinding
import com.lianbang.staff.dialog.ShipConfirmDialog
import com.lianbang.staff.module.main.OrderDetailActivity
import com.lianbang.staff.vm.ShopVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_shop_detail.*

class ShopDetailActivity :
    BaseListActivity<ShopVM, ActivityShopDetailBinding, ShopOrderListBean>()
    , OnItemClickListener, OnLoadMoreListener, OnRefreshListener {

    companion object {
        fun start(context: Context?, uid: Int, title: String) {
            context?.also {
                val starter = Intent(it, ShopDetailActivity::class.java)
                starter.putExtra("uid", uid)
                starter.putExtra("title", title)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                it.startActivity(starter)
            }
        }
    }

    private var mDetailBean: ShopDetailBean? = null
    private val mUid by lazy {
        intent.getIntExtra("uid", 0)
    }

    override fun initLayout(): Int {
        return R.layout.activity_shop_detail
    }

    override fun initData(intent: Intent) {
        super.initData(intent)
        mViewModel.mLiveData.observe(this, Observer<Any> {
            if (it is ShopDetailBean) {
                mDetailBean = it
                tv_shop_state.text = "门店资质状态：${if (it.is_audited == 1) "审核通过" else "审核未通过"}"
                tv_contact_person.text = "采购联系人：${it.real_name}"
                tv_phone.text = "联系方式：${it.phone}"
                tv_address.text = "地址：${it.addres}"
            }
        })
    }

    override fun initView() {
        super.initView()
        tv_toolbar_title.text = intent.getStringExtra("title")
        iv_img.setImageResource(R.color.black)
        btn_look.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                ToastUtils.toast("查看营业执照")
            }
        })

    }

    override fun loadData() {
        mViewModel.refreshData(ShopVM.TYPE_SHOP_ORDER_LIST, mUid)
        mViewModel.getShopDetail(mUid)
    }

    override fun initAdapter(): BaseQuickAdapter<ShopOrderListBean, *> {
        return ShopDetailListAdapter()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_shop_detail, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btn_call -> {
                mDetailBean?.apply {
                    val intent = Intent(
                        Intent.ACTION_CALL,
                        Uri.parse("tel:$phone")
                    )
                    startActivity(intent)
                }
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val shopOrderListBean = mListAdapter.data[position]
        OrderDetailActivity.start(
            this,
            shopOrderListBean.id,
            intent.getStringExtra("title"),
            mDetailBean?.phone ?: ""
        )
    }

    override fun onLoadMore() {
        mViewModel.loadData(ShopVM.TYPE_SHOP_ORDER_LIST, mUid)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.refreshData(ShopVM.TYPE_SHOP_ORDER_LIST, mUid)
    }
}
